import 'package:dio/dio.dart';

class NetUtils {
  // /authorize?response_type=code&client_id=s6BhdRkqt3&state=xyz&redirect_uri=https
  static Future<String> get(String url, Map<String, dynamic> params) async {
    if (url != null && params != null && params.isNotEmpty) {
      //拼装参数

      StringBuffer sb = StringBuffer("?");

      params.forEach((key, value) {
        sb.write("$key=$value&");
      });

      //去掉最后一个&
      String paramStr = sb.toString().substring(0, sb.length - 1);
      url += paramStr;
      print("url -> " + url);
    }

    //http.Response response = await http.get(Url(url));
    Response response = await Dio().get(url);
    print("response = $response");
    return response.toString();
  }

  static Future<String> post(String url, Map<String, dynamic> params) async {
    if (url != null && params != null && params.isNotEmpty) {
      //拼装参数

      StringBuffer sb = StringBuffer("?");

      params.forEach((key, value) {
        sb.write("$key=$value&");
      });

      //去掉最后一个&
      String paramStr = sb.toString().substring(0, sb.length - 1);
      url += paramStr;
      print(url);
    }

    //http.Response response = await http.get(Url(url));
    var response = await Dio().post(url);
    print("url=$url  response = $response");
    return response.toString();
  }
}
